The following commands reindent C constructs. Note that when you change your coding style, either interactively or through some other means, your file does not automatically get reindented. You will need to execute one of the following commands to see the effects of your changes.
Also, variables like
c-hanging-* and c-cleanup-list (see
Custom
Auto-newlines) only affect how on-the-fly code is formatted.
Changing the “hanginess” of a brace and then
reindenting, will not move the brace to a different line. For
this, you're better off getting an external program like GNU
indent, which will rearrange brace location, amongst
other things.
Preprocessor directives are handled as syntactic whitespace from other code, i.e. they can be interspersed anywhere without affecting the indentation of the surrounding code, just like comments.
The code inside macro definitions is, by default, still analyzed syntactically so that you get relative indentation there just as you'd get if the same code was outside a macro. However, since there is no hint about the syntactic context, i.e. whether the macro expands to an expression, to some statements, or perhaps to whole functions, the syntactic recognition can be wrong. CC Mode manages to figure it out correctly most of the time, though.
Reindenting large sections of code can take a long time. When CC Mode reindents a region of code, it is essentially equivalent to hitting <TAB> on every line of the region.
These commands indent code:
c-indent-command)c-indent-command does different things,
depending on the setting of
c-syntactic-indentation (see Indentation
Engine Basics):
nil (which it normally is),
the command indents the line according to its syntactic
context. With a prefix argument (C-u
<TAB>), it will re-indent the entire
expression1 that begins at the line's left
margin.nil, the command indents the
line by an extra c-basic-offset columns. A
prefix argument acts as a multiplier. A bare prefix
(C-u <TAB>) is equivalent to -1, removing
c-basic-offset columns from the
indentation.The precise behavior is modified by several variables:
With c-tab-always-indent, you can make
<TAB> insert whitespace in some
circumstances—c-insert-tab-function then
defines precisely what sort of “whitespace” this
will be. Set the standard Emacs variable
indent-tabs-mode to t if you want
real ‘tab’
characters to be used in the indentation, to nil
if you want only spaces. See Just Spaces.
This variable modifies how <TAB> operates.
- When it is
t(the default), <TAB> simply indents the current line.- When it is
nil, <TAB> (re)indents the line only if point is to the left of the first non-whitespace character on the line. Otherwise it inserts some whitespace (a tab or an equivalent number of spaces - see below) at point.- With some other value, the line is reindented. Additionally, if point is within a string or comment, some whitespace is inserted.
When “some whitespace” is inserted as described above, what actually happens is that the function stored in
c-insert-tab-functionis called. Normally, this isinsert-tab, which inserts a real tab character or the equivalent number of spaces (depending onindent-tabs-mode). Some people, however, setc-insert-tab-functiontotab-to-tab-stopso as to get hard tab stops when indenting.
The kind of indentation the next five
commands do depends on the setting of
c-syntactic-indentation (see Indentation
Engine Basics):
nil (the default), the commands
indent lines according to their syntactic context;nil, they just indent each line the
same amount as the previous non-blank line. The commands that
indent a region aren't very useful in this case.newline-and-indent)c-indent-exp)c-indent-defun)indent-region)c-mark-function)c-indent-defun, this command
operates on top-level constructs, and can't be used to mark
say, a Java method.These variables are also useful when indenting code:
This is a standard Emacs variable that controls how line indentation is composed. When it's non-
nil, tabs can be used in a line's indentation, otherwise only spaces are used.
When indenting large regions of code, this variable controls how often a progress message is displayed. Set this variable to
nilto inhibit the progress messages, or set it to an integer which is how often (in seconds) progress messages are to be displayed.
[1] this is only useful for a line starting with a comment opener or an opening brace, parenthesis, or string quote.